package common.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import common.bean.ResultJsonWrap;
import common.constant.GlobalConstant;

/**
 * 全局的Controller
 * 
 * @author liuxj
 * @version 1.0.0 2017-07-12
 */
@Controller
public class GlobalController {
    private Logger logger = Logger.getLogger(this.getClass());
    
//    @Autowired
//    private EhCacheCacheManager ehCacheCacheManager;

    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request) {
        return GlobalConstant.INDEX;
    }

    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request) {
        return GlobalConstant.LOGIN;
    }

    @RequestMapping(value = "/error")
    public String error(HttpServletRequest request) {
        return GlobalConstant.ERROR;
    }

    /**
     * 动态刷新Spring配置
     * 
     * @return 结果数据
     */
    @ResponseBody
    @RequestMapping(value = "/refreshApplicationContext")
    public ResultJsonWrap refreshApplicationContext() {
        logger.debug("refeshApplicationContext");

        ResultJsonWrap result = new ResultJsonWrap();
        try {
            logger.debug("开始刷新Spring配置！");
            
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            ((XmlWebApplicationContext) wac).refresh();
            
            logger.debug("Spring配置刷新完成！");
        } catch (Exception e) {
            logger.debug(e.getMessage(), e);
            result.setErrorMsg(e.getMessage());
        }

        return result;
    }
}
